package ace.handling;

import gov.nist.atlas.Anchor;
import gov.nist.atlas.Annotation;
import gov.nist.atlas.Id;
import gov.nist.atlas.Region;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

	public class EntityFactory {

		private Hashtable<Id, Entity> entities;
		private Document doc;
		private Hashtable<Id, Entity> realEntities;
		
		public EntityFactory(Document document) {
			
			entities = new Hashtable<Id, Entity>();
			
			realEntities = new Hashtable<Id, Entity>();
			
			this.doc = document;
			
		}

		public Entity generateEntity(Annotation mention) {
			
			Id id = mention.getId();
			
			Entity e = entities.get(id);
			
			if (e == null){
				
				e = new Entity(id);
				
				entities.put(id,e);

				if (mention.getContent().getContentType().getName().equals("ace_edt-content")){
				
					e.type = mention.getContent().getParameterWithRole("ace_edt-type").getValueAsString();
					
					e.root = e;

				} else {
				
					realEntities.put(id, e);
					
					Region full = mention.getRegion().getRegionWithRole("full");
					
					Anchor fullStart = full.getAnchorWithRole("start");
					
					e.fullstart = Integer.valueOf(fullStart.getParameterWithRole("char").getValueAsString());
					
					Anchor fullEnd = full.getAnchorWithRole("end");
					
					e.fullend = Integer.valueOf(fullEnd.getParameterWithRole("char").getValueAsString());
					
					Region head = mention.getRegion().getRegionWithRole("head");
					
					Anchor headStart = head.getAnchorWithRole("start");
					
					e.headstart = Integer.valueOf(headStart.getParameterWithRole("char").getValueAsString());
					
					Anchor headEnd = head.getAnchorWithRole("end");
					
					e.headend = Integer.valueOf(headEnd.getParameterWithRole("char").getValueAsString());
					
					e.doc = doc;
				
				}
			
			}
			
			return e;

			
		}

		public Enumeration<Entity> getEntities() {
			return realEntities.elements();
		}
		
	}